package com.authine.cloudpivot.ext.applicationservice;

import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.fastjson.JSONObject;
import com.authine.cloudpivot.ext.constants.ModelConst;
import com.authine.cloudpivot.ext.dto.BudgetDto;
import com.authine.cloudpivot.ext.utils.BoServiceUtils;
import com.authine.cloudpivot.ext.utils.BoUtils;
import com.authine.hermes.app.launcher.service.ApplicationService;
import com.authine.mvp.app.launcher.domain.SelectorFormat;
import com.authine.mvp.app.launcher.dto.bo.response.BO;
import com.authine.mvp.app.launcher.dto.bo.response.BOList;
import com.google.common.collect.Maps;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.authine.hermes.app.launcher.service.ApplicationServiceFoundation.CUSTOM_SERVICE;

/**
 * @ClassName BudgetService
 * @Description: 预算管理
 * @Author fj
 * @Date 2021/5/4
 * @Version V1.0
 **/
@Path("BudgetService")
@Slf4j
public class BudgetService extends ApplicationService {
    /**
     * 预算管理修改
     *
     * @param budgetDto 请求参数
     * @return  响应结果
     */
    @Path("updateBudget")
    @POST
    @Operation(summary = "预算管理修改", tags = CUSTOM_SERVICE)
    public SingleResponse<BO> updateBudget(@Parameter BudgetDto budgetDto) {
        if (StringUtils.isEmpty(budgetDto.getId())) {
            return SingleResponse.error("-1", "id不能为空");
        }
        SingleResponse<BOList> budget = BoServiceUtils.findListBySimple(ModelConst.T_BUDGET, "id", budgetDto.getId());
        log.info("--------Query the budget through id,and the budget result is：{}----------", JSONObject.toJSONString(budget));
        if (budget.getData().getTotal() == 0) {
            return SingleResponse.error("-2", "ID:" + budgetDto.getId() + "不存在");
        }
        if (budgetDto.getAvailable_limit().compareTo(budgetDto.getBudget_limit()) > 0) {
            return SingleResponse.error("-3", "预算可用额度不可大于预算限额");
        }
        // 修改预算管理
        Map<String, Object> budgetMap = BoUtils.bo2Map(budgetDto);
        SingleResponse<BO> budgetResult = BoServiceUtils.updateMainBo(ModelConst.T_BUDGET, budgetMap);
        log.info("--------budgetResult：{}----------", JSONObject.toJSONString(budgetResult));

        // 保存预算变更记录
        saveBudgetRecord(budget.getData().getData().get(0), budgetDto);
        return budgetResult;
    }

    /**
     * 预算管理记录变更
     *
     * @param budgetMap 修改前数据
     * @param newBudget 修改后数据
     */
    @SuppressWarnings("unchecked")
    private void saveBudgetRecord(Map<String, Object> budgetMap, BudgetDto newBudget) {
        BudgetDto oldBudget = new BudgetDto();
        List<SelectorFormat> organization = (List<SelectorFormat>) budgetMap.get("organization_id");
        List<SelectorFormat> department = (List<SelectorFormat>) budgetMap.get("department_id");
        oldBudget.setOrganization_id(organization.get(0));
        oldBudget.setDepartment_id(department.get(0));
        oldBudget.setAvailable_limit((BigDecimal) budgetMap.get("available_limit"));
        oldBudget.setBudget_limit((BigDecimal) budgetMap.get("budget_limit"));
        log.info("-----------oldBudget：{}-------------", JSONObject.toJSONString(oldBudget));
        String summary = "使用人主动修改信息";
        List<Map<String, Object>> data = new ArrayList<>();
        Map<String, Object> dataMap;
        if (!newBudget.getOrganization_id().equals(oldBudget.getOrganization_id())) {
            dataMap = Maps.newHashMap();
            dataMap.put("budget_id", newBudget.getId());
            dataMap.put("column_name", "organization_id");
            dataMap.put("new_value", newBudget.getOrganization_id());
            dataMap.put("old_value", oldBudget.getOrganization_id());
            dataMap.put("summary", summary);
            data.add(dataMap);
        }
        if (!newBudget.getDepartment_id().equals(oldBudget.getDepartment_id())) {
            dataMap = Maps.newHashMap();
            dataMap.put("budget_id", newBudget.getId());
            dataMap.put("column_name", "department_id");
            dataMap.put("new_value", newBudget.getDepartment_id());
            dataMap.put("old_value", oldBudget.getDepartment_id());
            dataMap.put("summary", summary);
            data.add(dataMap);
        }
        if (!newBudget.getAvailable_limit().equals(oldBudget.getAvailable_limit())) {
            dataMap = Maps.newHashMap();
            dataMap.put("budget_id", newBudget.getId());
            dataMap.put("column_name", "available_limit");
            dataMap.put("new_value", newBudget.getAvailable_limit());
            dataMap.put("old_value", oldBudget.getAvailable_limit());
            dataMap.put("summary", summary);
            data.add(dataMap);
        }
        if (!newBudget.getBudget_limit().equals(oldBudget.getBudget_limit())) {
            dataMap = Maps.newHashMap();
            dataMap.put("budget_id", newBudget.getId());
            dataMap.put("column_name", "budget_limit");
            dataMap.put("new_value", newBudget.getBudget_limit());
            dataMap.put("old_value", oldBudget.getBudget_limit());
            dataMap.put("summary", summary);
            data.add(dataMap);
        }
        BoServiceUtils.createBo(ModelConst.T_BUDGET_RECORD, data);
    }
}
